<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>lunar-灶马头</title>
<meta name="keywords" content="lunar,阳历,阴历,公历,农历,佛历,道历" />
<meta name="description" content="lunar是一个无依赖的支持阳历、阴历、佛历和道历的日历工具库。" />
<meta name="author" content="6tail" />
<link rel="stylesheet" href="common.css">
<link rel="stylesheet" href="highlight.min.css">
<link rel="stylesheet" href="markdown.css">
<link rel="stylesheet" href="page.css">
<style>
.markdown-body table{
  width:auto;
}
.markdown-body table td{
  text-align:center;
  padding: 15px;
}
.bold{font-weight:bold;}
#year {
  font-size:1.2em;
}
.bar{
  text-align:center;
  height:30px;
  line-height:30px;
}
</style>
</head>
<body>
<div class="article">
<textarea class="md">
## 灶马头

</textarea>
<div class="bar">
  <a href="javascript:void(0);" id="btn-prev" target="_self">&lt;&lt; 上一年</a>
  &nbsp;&nbsp;&nbsp;
  <a href="javascript:void(0);" id="btn-next" target="_self">下一年 &gt;&gt;</a>
</div>
<div>
  <table>
    <tbody>
      <tr>
        <td colspan="13" id="year"></td>
      </tr>
      <tr id="month">
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td class="bold"><div>阴历</div></td>
      </tr>
      <tr id="jieqi">
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td class="bold"><div>节气</div></td>
      </tr>
      <tr id="fu">
        <td class="bold"></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td id="taisui"></td>
        <td id="guishen"></td>
        <td id="fushen"></td>
        <td id="xishen"></td>
        <td id="caishen"></td>
        <td class="bold"><div>农事</div></td>
      </tr>
    </tbody>
  </table>
</div>
</div>
<script src="libs/jquery.min.js"></script>
<script src="libs/highlight.min.js"></script>
<script src="libs/marked.min.js"></script>
<script src="common-source.js"></script>
<script src="lunar.js"></script>
<script>
mdr('.md');
</script>
<script>
(function(){
  var ExactDate = (function(){
    return {
      _:function(date,y,m,d){
        if(y<100){
          date.setFullYear(y);
          date.setMonth(m-1);
          date.setDate(d);
        }
        date.setMilliseconds(0);
        return date;
      },
      fromYmd:function(y,m,d){
        return this.fromYmdHms(y,m,d,0,0,0);
      },
      fromYmdHms:function(y,m,d,hour,minute,second){
        return this._(new Date(y+'/'+m+'/'+d+' '+hour+':'+minute+':'+second),y,m,d);
      }
    };
  })();
  
  var convertJieQi = function(name) {
    var jq = name;
    if('DONG_ZHI'===jq){
      jq = '冬至';
    }else if('DA_HAN'===jq){
      jq = '大寒';
    }else if('XIAO_HAN'===jq){
      jq = '小寒';
    }else if('LI_CHUN'===jq){
      jq = '立春';
    }else if('DA_XUE'===jq){
      jq = '大雪';
    }else if('YU_SHUI'===jq){
      jq = '雨水';
    }else if('JING_ZHE'===jq){
      jq = '惊蛰';
    }
    return jq;
  };
  
function generate(solarYear){
  var solar = Solar.fromYmd(solarYear.getYear(), 6, 1);
  var lunar = solar.getLunar();
  $('#year').html(lunar.getYearInChinese() + '年岁次' + lunar.getYearInGanZhi());
  var year = lunar.getYear();
  var lunarYear = LunarYear.fromYear(year);
  var lunarMonths = lunarYear.getMonths();
  var notLeapLunarMonths = [];
  for (var i = 0, j = lunarMonths.length; i < j; i++) {
    var m = lunarMonths[i];
    if (m.getYear() === year && !m.isLeap()) {
      notLeapLunarMonths.push(m);
    }
  }
  var monthCount = notLeapLunarMonths.length;
  for (var i = 0; i < monthCount; i++) {
    var m = notLeapLunarMonths[i];
    $('#month td').eq(11-i).html('<div>' + LunarUtil.MONTH[m.getMonth()] + '月' + (m.getDayCount() > 29 ? '大' : '小') + '</div>');
  }
  var jieQiTable = lunar.getJieQiTable();
  var jieQiList = lunar.getJieQiList();
  var jieQis = [];
  for (var i = 0; i < monthCount; i++) {
    var jqs = [];
    for (var x = 0, y = jieQiList.length; x < y; x++) {
      var name = jieQiList[x];
      var s = jieQiTable[name];
      var l = s.getLunar();
      if (l.getYear() === year && l.getMonth() === i + 1) {
        jqs.push('<div>' + l.getDayInChinese() + convertJieQi(name) + '</div>');
      }
    }
    jieQis.push(jqs);
  }
  
  for (var i = 0; i < monthCount; i++) {
    var jqs = jieQis[i];
    if (jqs) {
      $('#jieqi td').eq(11-i).html(jqs.join(''));
    }
  }
  
  var getFus = function() {
    var l = [];
    var xiaZhi = jieQiTable['夏至'];
    var liQiu = jieQiTable['立秋'];
    var startCalendar = ExactDate.fromYmd(xiaZhi.getYear(), xiaZhi.getMonth(), xiaZhi.getDay());

    // 第1个庚日
    var add = 6 - xiaZhi.getLunar().getDayGanIndex();
    if (add < 0) {
      add += 10;
    }
    // 第3个庚日，即初伏第1天
    add += 20;
    startCalendar.setDate(startCalendar.getDate() + add);
    l.push({
      lunar: Lunar.fromDate(startCalendar),
      name: '初伏'
    });
    
    // 第4个庚日，中伏第1天
    startCalendar.setDate(startCalendar.getDate() + 10);
    l.push({
      lunar: Lunar.fromDate(startCalendar),
      name: '中伏'
    });
    
    // 第5个庚日，中伏第11天或末伏第1天
    startCalendar.setDate(startCalendar.getDate() + 10);
    
    var liQiuCalendar = ExactDate.fromYmd(liQiu.getYear(), liQiu.getMonth(), liQiu.getDay());
    if (liQiuCalendar > startCalendar) {
      // 末伏第1天
      startCalendar.setDate(startCalendar.getDate() + 10);
    }
    l.push({
      lunar: Lunar.fromDate(startCalendar),
      name: '末伏'
    });
    return l;
  };
  
  // 三伏
  var fuList = getFus();
  var fus = [];
  for (var i = 0; i < monthCount; i++) {
    var fs = [];
    for (var x = 0, y = fuList.length; x < y; x++) {
      var fu = fuList[x];
      if (fu.lunar.getYear() === year && fu.lunar.getMonth() === i + 1) {
        fs.push('<div>' + fu.lunar.getDayInChinese() + fu.name + '</div>');
      }
    }
    fus.push(fs);
  }
  
  for (var i = 0; i < monthCount; i++) {
    var l = fus[i];
    if (l) {
      $('#fu td').eq(11-i).html(l.join(''));
    }
  }
  
  var fuTds = $('#fu td');
  
  fuTds.eq(0).html('<div>廿三祭灶</div>');
  fuTds.eq(1).html('<div>' + lunarYear.getKanCan() + '</div><div>' + lunarYear.getDeJin() + '</div>' + fuTds.eq(1).html());
  fuTds.eq(2).html('<div>' + lunarYear.getCaoZi() + '</div><div>' + lunarYear.getHuaShou() + '</div>' + fuTds.eq(2).html());
  fuTds.eq(3).html('<div>' + lunarYear.getRenChu() + '</div><div>' + lunarYear.getRenBing() + '</div>' + fuTds.eq(3).html());
  fuTds.eq(4).html('<div>' + lunarYear.getJiaTian() + '</div><div>' + lunarYear.getGengTian() + '</div>' + fuTds.eq(4).html());
  fuTds.eq(5).html('<div>' + lunarYear.getZhiShui() + '</div>' + fuTds.eq(5).html());
  
  $('#caishen').html('<div>财神' + lunarYear.getPositionCaiDesc() + '</div>');
  $('#xishen').html('<div>喜神' + lunarYear.getPositionXiDesc() + '</div>');
  $('#fushen').html('<div>福神' + lunarYear.getPositionFuDesc() + '</div>');
  $('#guishen').html('<div>贵神' + lunarYear.getPositionYangGuiDesc() + '</div>');
  $('#taisui').html('<div>太岁' + lunarYear.getPositionTaiSuiDesc() + '</div>');
}

  var year = SolarYear.fromDate(new Date());
  
  generate(year);
  
  document.getElementById('btn-prev').onclick = function(){
    year = year.next(-1);
    generate(year);
  };
  
  document.getElementById('btn-next').onclick = function(){
    year = year.next(1);
    generate(year);
  };
  
})();
</script>
</body>
</html>